package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Classinfo;
import cn.wolfcode.crm.query.ClassinfoQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClassinfoService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {

    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IEmployeeService employeeService;

    //提供一个方法处理查询所有部门的请求,响应HTML
    @RequestMapping("/list")
   // @RequiredPermission(name = "部门列表")
    @RequiresPermissions(value = {"classinfo:list","班级列表"}, logical=Logical.OR)//必须要有指定的权限才能访问该方法,所以之前的注解可以删了
    //要贴上注解,因为页面要取值的话不贴@ModelAttribute去到的值是${queryObject.xxx},首字符会小写
    public String list(Model model, @ModelAttribute("qo") ClassinfoQuery qo) {//创建qo对象
        //这里不再是调用listAll的方法,而是我嗯创建的query方法
        // List<Classinfo> classinfos = classinfoService.listAll();
        PageInfo pageInfo = classinfoService.query(qo);//这里没有qo对象需要我们在上面创建,返回的方法是pageResult
        model.addAttribute("result", pageInfo);
        model.addAttribute("employee",employeeService.listAll());
        return "classinfo/list";// /WEB-INF/views/classinfo/potentialList.ftl
    }

/*    @RequestMapping("/input")一步请求使用模态框弹出
    @RequiredPermission(name = "部门新增/编辑")
    public String input(Long id,Model model){
        //判断id是否为空
        if (id != null) {
            Classinfo classinfo = classinfoService.get(id);
            model.addAttribute("classinfo",classinfo);
        }
        return "classinfo/input";// /WEB-INF/views/classinfo/input.ftl
    }*/

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"saveOrUpdate:list","班级保存/更新"}, logical=Logical.OR)//or只要有一个就可以运行
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo) {
        try {
            if (classinfo.getId() == null) {
                classinfoService.save(classinfo);
            } else {
                classinfoService.update(classinfo);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new JsonResult(false, "操作失败");
        }
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"delete:list","班级保存/更新"}, logical=Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        try {//加try,catch是为了用户体验,当某个地方出错的时候可以弹出一个异常出来
            // 报200错误有返回的结果,反之报错500没有直接停止

            classinfoService.delete(id);

            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new JsonResult(false, "操作失败");
        }
    }
}
